from django import http
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django import newforms as forms
from django.conf import settings
import time
import datetime
from mysite.test.models import Image
from mysite.test.models import Taichuang
from mysite.test.models import Taipei
from mysite.test.models import Six
from mysite.test.models import Seven
from mysite.test.models import Mobile
from mysite.test.models import PlayGround
from mysite.test.models import tagList
from mysite.test.models import Story

from django.template import Context, loader

image_dir = 'data'
_tag = ''

def tag(request):
    response = HttpResponse()
    _tag = str(request.POST['tag'])
    arr = _tag.split(',')
    Min=10000
    minTag=''
    f=open('output.txt', 'w')
    im = Image.objects.all()
    _path=''
    for i in range(im.count()):
        tmp = im[i].tag
        flag=1
        for j in range(len(arr)-1):
            if tmp.count(arr[j])==0:
                flag=0
        if flag==1:
            _path = _path + im[i].path + ',' 
    
    f.close()        
    response.write(_path)
    return response
    
def video_info(request):
    response = HttpResponse()
    video = str(request.POST['video'])
    video = video+'.flv'
    Im = Image.objects.get(path=video)
    response.write(Im.name+'||'+Im.des+'||'+Im.tag)
    return response
    
def story_info(request):
    response = HttpResponse()
    st = Story.objects.get(title=request.POST['story'])
    response.write( st.title+'||'+st.des+'||'+st.tag )
    return response
    
def upload_reply(request):
    response = HttpResponse()
    response.write('success')
    return response
   
def upload_image(request):
    f=open('output.txt', 'w')
    f.write(str(request.POST['des']))
    f.write(str(request.POST['image_name']))
    
    if request.method == 'POST':
        _time = time.strftime("%m-%d-%Y-%H-%M-%S", time.gmtime())
        filetype = request.FILES['Filedata']['filename'].split('.')
        filename = _time + '.' + filetype[1].lower()
        fd = open('%s/%s' %(image_dir, filename), 'wb')
        fd.write(request.FILES['Filedata']['content'])
        fd.close()
        
        _tag = ''
        
        if request.POST['Taichuang']=='true':
            _tag = _tag + 'Taichuang' +','
            Taichuang(path=filename).save()

        if request.POST['Taipei'] == 'true':
            _tag = _tag + 'Taipei' +','
            T2 = Taipei(path=filename)
            T2.save() 
        if request.POST['Mobile'] == 'true':
            _tag = _tag + 'Mobile' +','
            T3 = Mobile(path=filename)
            T3.save()
        if request.POST['PlayGround'] == 'true':
            _tag = _tag + 'PlayGround' +','
            T4 = PlayGround(path=filename)
            T4.save()
        if request.POST['Six'] == 'true':
            _tag = _tag + 'Six' +','
            T5 = Six(path=filename)
            T5.save()
        if request.POST['Seven'] == 'true':
            _tag = _tag + 'Seven' +','
            T6 = Seven(path=filename)
            T6.save()
        
        
        f.write(_tag)   
        I = Image(name=request.POST['image_name'], path=filename, des=request.POST['des'], tag=_tag)
        I.save()
        
    f.close()
    return
    
def tagListRequest(request):
    response = HttpResponse()
    arr = tagList.objects.all()
    list = ''
    for i in range(arr.count()):
        if i==(arr.count()-1):
            list = list+arr[i].tag
        else:
            list = list+arr[i].tag+'||'
    response.write(list)
    return response
        
def submitStory(request):
    response = HttpResponse()
    myTag = request.POST['mytag']
    tagArray = request.POST['createtag'].split(',')
    for i in tagArray:
        if(i!=''):
            ct = tagList(tag=i)
            ct.save()
            myTag = myTag + i + ','
            
    st = Story(des=request.POST['des'], title=request.POST['title'], list=request.POST['list'], tag=myTag, subtitle=request.POST['subtitle'], time=datetime.datetime.now())
    st.save()
    response.write('1')
    return response
    
def story_list(request):
    response = HttpResponse()
    st = Story.objects.all()
    titles = ''
    for i in range(st.count()):
        titles = titles + st[i].title + ','
    
    response.write(titles)
    return response

def story_request(request):
    response = HttpResponse()
    _path = ''
    st = Story.objects.get(title=request.POST['story'])
    response.write(st.list)
    f = open('output.txt', 'w')
    f.write(st.list)
    f.close()
    return response
